package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomerDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/customers")
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @GetMapping
    public Result listCustomer(CustomerDto customerDto){
        log.info("查询客户列表数据{}", customerDto);
        PageResult<Customer> pageResult = customerService.listCustomer(customerDto);
        return Result.success(pageResult);

    }
    @PostMapping
    public Result addCustomer(@RequestBody Customer customer){
        log.info("添加客户数据{}", customer);
        customerService.save(customer);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getCustomerById(@PathVariable Integer id){
        log.info("查询客户id{}", id);
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }
    @PutMapping
    public Result updateCustomer(@RequestBody Customer customer){
        log.info("更新客户数据{}", customer);
        customerService.updateById(customer);
        return Result.success();
    }
}
